﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraPrinting;

namespace MINERP.Statistics.ShopBill
{
    public partial class FrmBillStatistics : Form
    {
        private static   FrmBillStatistics _instance = null;
        DataSet ds =null;

        public static FrmBillStatistics Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new FrmBillStatistics();
                }
                return _instance;
            }
            set
            {
                _instance = value;
            }
        }

        private BLL.Statistics.ShopBillIndexBLL bll = new MINERP.BLL.Statistics.ShopBillIndexBLL();
        public FrmBillStatistics()
        {
            InitializeComponent();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                btnOK.Enabled = false;
                FindBill();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            finally
            {
                btnOK.Enabled = true;
            }
        }

        private void FrmBillStatistics_Load(object sender, EventArgs e)
        {
            try
            {
                dtStartTime.DateTime = DateTime.Now.Date;
                dtEndTime.DateTime = DateTime.Now.AddDays(1).Date.AddSeconds(-1);
                cmbStatus.Text = "已付款";
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message, "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void FindBill()
        {
            try
            {
                DateTime start = dtStartTime.DateTime.Date;
                DateTime end = dtEndTime.DateTime.AddDays(1).Date.AddSeconds(-1);
                if (start >= end.AddDays(1))
                {
                    MessageBox.Show("时间范围不合要求！", "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                string payType = cmbStatus.Text.Trim();
                int status = 0;
                switch (payType)
                {
                    case "全部":
                        status = 2; break;//未付款和已付款账单
                    case "未付款":
                        status = (int)COM.ShopBillStatus.UnCheckOut; break;
                    case "已付款":
                        status = (int)COM.ShopBillStatus.CheckedOut; break;
                }
                int timeType = 0;
                if (cmbBilltime.Text == "结账时间")
                {
                    timeType = 0;
                }
                else
                {
                    timeType = 1;
                }
                ds = bll.GetBillStatistics(start, end, status, timeType);
                if (ds != null)
                {
                    ds.WriteXml(@"C:\WINDOWS\xmlData\BillStatistics.xml", XmlWriteMode.WriteSchema);
                    report.Preview = previewControl1;
                    report.RegisterData(ds);
                    report.SetParameterValue("startTime", start);
                    report.SetParameterValue("endTime", end);
                    string a = report.GetParameterValue("startTime").ToString();
                    report.Load(Application.StartupPath + "\\Report\\BillStatistics.frx");
                    report.Show();
                }
                else
                {
                    MessageBox.Show("没有符合条件的数据!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void previewControl2_Load(object sender, EventArgs e)
        {

        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            try
            {
                if (ds == null || ds.Tables.Count == 0)
                {
                    MessageBox.Show("没有数据！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }
                else
                {
                    gridControlForExport.DataSource = ds.Tables[0];

                    SaveFileDialog saveFileDialog = new SaveFileDialog();
                    saveFileDialog.Filter = "文本文件 (*.txt)|*.txt|pdf(*.pdf)|*.pdf|电子表格(*.xls)|*.xls|html(*.html)|*.html";
                    //saveFileDialog.AddExtension = true;
                    saveFileDialog.FilterIndex = 3;
                    saveFileDialog.RestoreDirectory = true;
                    if (saveFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        string fileName = saveFileDialog.FileName;
                        if (saveFileDialog.OpenFile() != null)
                        {
                            switch (saveFileDialog.FilterIndex)
                            {
                                case 3:
                                    //XlsExportOptions xo = new XlsExportOptions(true, false, false, false, false);
                                    DevExpress.XtraPrinting.XlsExportOptions options = new DevExpress.XtraPrinting.XlsExportOptions();
                                    gridControlForExport.ExportToXls(fileName);
                                    //gridViewForEX.ExportToXls(fileName, xo);
                                    //gridViewForEX.ExportToXls(
                                    return;
                                case 4:
                                    gridView1.ExportToHtml(fileName);
                                    return;
                                case 2:
                                    gridView1.ExportToPdf(fileName);
                                    return;
                                case 1:
                                    this.gridView1.ExportToText(fileName);
                                    return;
                                default:
                                    break;
                            }
                        }

                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message,"提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
        }

        private void FrmBillStatistics_FormClosing(object sender, FormClosingEventArgs e)
        {
            _instance = null;
        }
    }
}
